package br.com.quiz.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import br.com.quiz.domain.Alternativa;
import br.com.quiz.domain.Questao;
import br.com.quiz.util.HibernateUtil;

public class AlternativaDAO{
	public static Integer salvar( Alternativa alt ) {
		Session sessao = HibernateUtil.getSessionFactory().openSession();
		Transaction transacao = null;
		try {

			transacao = sessao.beginTransaction();
			Integer id = (Integer) sessao.save( alt );
			transacao.commit();
			return id;

		} catch ( Exception e ) {
			transacao.rollback();
			e.printStackTrace();

		}
		return null;

	}

	public static List< Alternativa > listar() {
		Session sessao = HibernateUtil.getSessionFactory().openSession();
		List< Alternativa > alternativa = null;
		try {
			Query consulta = sessao.getNamedQuery( "Alternativa.listar" );
			alternativa = consulta.list();
		} catch ( RuntimeException e ) {
			throw e;
		} finally {
			sessao.close();
		}

		return alternativa;
	}

	public static Alternativa filtrarPorId( Integer idAlternativaA ) {
		Session sessao = HibernateUtil.getSessionFactory().openSession();
		Alternativa alternativa = null;
		try {
			Query consulta = sessao.getNamedQuery( "Alternativa.filtrarPorId" );
			consulta.setLong( "altId", idAlternativaA );
			alternativa = (Alternativa) consulta.uniqueResult();
		} catch ( RuntimeException e ) {
			throw e;
		} finally {
			sessao.close();
		}

		return alternativa;
	}
}
